package com.rongji.cms.tools.server.test;//package com.rongji.cms.tools.flowable.test;
//
//import io.swagger.annotations.Api;
//import io.swagger.annotations.ApiOperation;
//import lombok.extern.slf4j.Slf4j;
//import org.flowable.bpmn.model.BpmnModel;
//import org.flowable.engine.*;
//import org.flowable.engine.runtime.Execution;
//import org.flowable.engine.runtime.ProcessInstance;
//import org.flowable.image.ProcessDiagramGenerator;
//import org.flowable.task.api.Task;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.web.bind.annotation.GetMapping;
//import org.springframework.web.bind.annotation.RequestMapping;
//import org.springframework.web.bind.annotation.RequestParam;
//import org.springframework.web.bind.annotation.RestController;
//
//import javax.servlet.http.HttpServletResponse;
//import java.io.IOException;
//import java.io.InputStream;
//import java.io.OutputStream;
//import java.util.ArrayList;
//import java.util.HashMap;
//import java.util.List;
//
///**
// * @author yang
// * @date 2022/9/13 11:04
// * @description 测试用的
// */
//
//@Api(tags = "报销流程测试")
//@RestController
//@Slf4j
//@RequestMapping("/expense")
//public class ExpenseController {
//
//    @Autowired
//    private RuntimeService runtimeService;
//    @Autowired
//    private TaskService taskService;
//    @Autowired
//    private RepositoryService repositoryService;
//    @Autowired
//    private ProcessEngine processEngine;
//
//    /**
//     * 添加报销
//     *
//     * @param userId      用户id
//     * @param money       报销金额
//     * @param description 描述
//     * @return
//     */
//    @ApiOperation(value = "添加报销测试")
//    @GetMapping("/add")
//    public String addExpense(@RequestParam(value = "userId") String userId,
//                             @RequestParam(value = "money") String money,
//                             @RequestParam(value = "description") String description) {
//
//        HashMap<String, Object> map = new HashMap<>();
//        map.put("taskUser", userId);
//        map.put("money", money);
//        map.put("description", description);
//        ProcessInstance processInstance = runtimeService.startProcessInstanceByKey("expense", map);
//        return "提交成功.流程Id为：" + processInstance.getId();
//    }
//
//    /**
//     * 获取审批管理列表
//     */
//    @ApiOperation(value = "获取审批管理列表")
//    @GetMapping("/list")
//    public Object list(@RequestParam(value = "userId") String userId) {
//        List<Task> taskList = taskService.createTaskQuery().taskAssignee(userId).orderByTaskCreateTime().desc().list();
//
//        for (Task task : taskList) {
//            log.info("task:{}", task.toString());
//        }
//        return taskList.toString();
//    }
//
//    @ApiOperation(value = "批准")
//    @RequestMapping("/apply")
//    public String apply(@RequestParam(value = "taskId") String taskId) {
//        Task task = taskService.createTaskQuery().taskId(taskId).singleResult();
//        if (task == null) {
//            throw new RuntimeException("流程不存在");
//        }
//        HashMap<String, Object> map = new HashMap<>();
//        map.put("command", "通过");
//        taskService.complete(taskId, map);
//        return "批准";
//
//    }
//
//
//    @ApiOperation(value = "驳回")
//    @RequestMapping("/reject")
//    public String reject(@RequestParam(value = "taskId") String taskId) {
//        Task task = taskService.createTaskQuery().taskId(taskId).singleResult();
//        if (task == null) {
//            throw new RuntimeException("流程不存在");
//        }
//        HashMap<String, Object> map = new HashMap<>();
//        map.put("command", "驳回");
//        taskService.complete(taskId, map);
//        return "驳回";
//    }
//
//
//    @ApiOperation(value = "流程图")
//    @RequestMapping("/genProcessDiagram")
//    public void genProcessDiagram(HttpServletResponse httpServletResponse, @RequestParam(value = "processId") String processId) throws IOException {
//        ProcessInstance pi = runtimeService.createProcessInstanceQuery().processInstanceId(processId).singleResult();
//
//        //流程走完的不显示图
//        if (pi == null) {
//            return;
//        }
//        Task task = taskService.createTaskQuery().processInstanceId(pi.getId()).singleResult();
//        //使用流程实例ID，查询正在执行的执行对象表，返回流程实例对象
//        String InstanceId = task.getProcessInstanceId();
//        List<Execution> executions = runtimeService
//                .createExecutionQuery()
//                .processInstanceId(InstanceId)
//                .list();
////得到正在执行的Activity的Id
//        List<String> activityIds = new ArrayList<>();
//        List<String> flows = new ArrayList<>();
//        for (Execution exe : executions) {
//            List<String> ids = runtimeService.getActiveActivityIds(exe.getId());
//            activityIds.addAll(ids);
//        }
//
//
//        //获取流程图
//        BpmnModel bpmnModel = repositoryService.getBpmnModel(pi.getProcessDefinitionId());
//        ProcessEngineConfiguration engconf = processEngine.getProcessEngineConfiguration();
//        ProcessDiagramGenerator diagramGenerator = engconf.getProcessDiagramGenerator();
//        InputStream in = diagramGenerator.generateDiagram(bpmnModel, "png", activityIds, flows,
//                engconf.getActivityFontName(), engconf.getLabelFontName(),
//                engconf.getAnnotationFontName(), engconf.getClassLoader(), 1.0, true);
//        OutputStream out = null;
//        byte[] buf = new byte[1024];
//        int legth = 0;
//        try {
//            out = httpServletResponse.getOutputStream();
//            while ((legth = in.read(buf)) != -1) {
//                out.write(buf, 0, legth);
//            }
//        } catch (IOException e) {
//            e.printStackTrace();
//        } finally {
//            if (in != null) {
//                in.close();
//            }
//            if (out != null) {
//                out.close();
//            }
//        }
//    }
//
//
//}
